/*
 * Copyright 2011 Marcos Fabio Pereira
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.moveme.examples.ninjafruitlike;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.moveme.MoveMeClient;
import com.moveme.MoveMeListener;
import com.moveme.network.payload.PSPayload;
import com.moveme.network.payload.state.PSMoveState;
import java.awt.Font;
import java.util.TimerTask;

/**
 * MoveMe UI Console.
 * 
 * @author Marcos Fabio Pereira
 * 
 */
public class NinjaFruitLike extends JFrame implements ActionListener,
        ChangeListener, MoveMeListener {

    /**
     * JPanel that host the screen view.
     */
    private GameScreen gameScreen;

    /**
     * The MoveMeClient instance.
     */
    private MoveMeClient moveMeClient;

    /**
     * JPanel that host the position information about move controllers.
     */
    private JPanel spherePanel;

    /**
     * Move controller selected.
     */
    private short moveControllerSelected = 0;

    private JColorChooser sphereColorChooser;

    private JTextField ipServer;
    private JLabel lblNormalizedX;
    private JLabel lblNormalizedY;
    private JLabel lblValid;

    /**
     * Create the panel.
     */
    public NinjaFruitLike() {
        setMaximumSize(new Dimension(1080, 640));
        setMinimumSize(new Dimension(1080, 640));
        setSize(new Dimension(1080, 640));
        setTitle("MoveMe Console");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(1080, 640));
        getContentPane().setLayout(null);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(202, 15, 852, 552);
        tabbedPane.addChangeListener(this);
        getContentPane().add(tabbedPane);

        JPanel screenPanel = new JPanel();
        screenPanel.setName("screenPanel");
        tabbedPane.addTab("Camera", null, screenPanel, null);
        tabbedPane.setBackgroundAt(0, new Color(192, 192, 192));
        screenPanel.setLayout(null);

        gameScreen = new GameScreen();
        gameScreen.setBounds(3, 5, 617, 480);
        gameScreen.setPreferredSize(new Dimension(640, 480));
        
        JFrame gameWindow = new JFrame("Game Screen");
        gameWindow.getContentPane().add(gameScreen);
        gameWindow.pack();
        gameWindow.setVisible(true);

        lblValid = new JLabel("New label");
        lblValid.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblValid.setBounds(630, 177, 207, 25);
        screenPanel.add(lblValid);

        lblNormalizedX = new JLabel("New label");
        lblNormalizedX.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblNormalizedX.setBounds(630, 234, 207, 25);
        screenPanel.add(lblNormalizedX);

        lblNormalizedY = new JLabel("New label");
        lblNormalizedY.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblNormalizedY.setBounds(630, 297, 207, 25);
        screenPanel.add(lblNormalizedY);

        JPanel colorSphere = new JPanel();
        colorSphere.setLayout(null);
        tabbedPane.addTab("Sphere color", null, colorSphere, null);

        sphereColorChooser = new JColorChooser();
        sphereColorChooser.setColor(Color.WHITE);
        sphereColorChooser.setBounds(new Rectangle(10, 170, 182, 362));
        sphereColorChooser.setBounds(0, 0, 532, 362);
        sphereColorChooser.getSelectionModel().addChangeListener(this);
        colorSphere.add(sphereColorChooser);

        JButton btnInitButton = new JButton("Init");
        btnInitButton.setActionCommand("init");
        btnInitButton.addActionListener(this);
        btnInitButton.setBounds(6, 64, 89, 23);
        getContentPane().add(btnInitButton);

        ButtonGroup moveControllerGroup = new ButtonGroup();

        JRadioButton moveController0RadioButton = new JRadioButton(
                "Move controller 0");
        moveController0RadioButton.setSelected(true);
        moveController0RadioButton.setBounds(6, 131, 182, 23);
        moveController0RadioButton.setActionCommand("moveController0Selected");
        moveController0RadioButton.addActionListener(this);
        getContentPane().add(moveController0RadioButton);

        JRadioButton moveController1RadioButton = new JRadioButton(
                "Move controller 1");
        moveController1RadioButton.setBounds(6, 157, 182, 23);
        moveController1RadioButton.setActionCommand("moveController1Selected");
        moveController1RadioButton.addActionListener(this);
        getContentPane().add(moveController1RadioButton);

        JRadioButton moveController3RadioButton = new JRadioButton(
                "Move controller 3");
        moveController3RadioButton.setBounds(6, 209, 182, 23);
        moveController3RadioButton.setActionCommand("moveController3Selected");
        moveController3RadioButton.addActionListener(this);
        getContentPane().add(moveController3RadioButton);

        JRadioButton moveController2RadioButton = new JRadioButton(
                "Move controller 2");
        moveController2RadioButton.setBounds(6, 183, 182, 23);
        moveController2RadioButton.setActionCommand("moveController2Selected");
        moveController2RadioButton.addActionListener(this);
        getContentPane().add(moveController2RadioButton);

        moveControllerGroup.add(moveController0RadioButton);
        moveControllerGroup.add(moveController1RadioButton);
        moveControllerGroup.add(moveController2RadioButton);
        moveControllerGroup.add(moveController3RadioButton);

        ipServer = new JTextField();
        ipServer.setBounds(6, 34, 182, 20);
        ipServer.setText("192.168.0.103");
        getContentPane().add(ipServer);
        ipServer.setColumns(10);

        JLabel lblPsMoveServer = new JLabel("Type the PS3 address"); //$NON-NLS-1$
        lblPsMoveServer.setBounds(6, 15, 182, 14);
        getContentPane().add(lblPsMoveServer);

        JLabel lblSelectAController = new JLabel("Choose a move controller"); //$NON-NLS-1$
        lblSelectAController.setBounds(10, 110, 174, 14);
        getContentPane().add(lblSelectAController);

        JButton btnLeftPosition = new JButton("left");
        btnLeftPosition.setBounds(10, 306, 55, 23);
        btnLeftPosition.setActionCommand("leftPosition");
        btnLeftPosition.addActionListener(this);
        getContentPane().add(btnLeftPosition);

        JButton btnRightPosition = new JButton("right");
        btnRightPosition.setBounds(129, 306, 55, 23);
        btnRightPosition.setActionCommand("rightPosition");
        btnRightPosition.addActionListener(this);
        getContentPane().add(btnRightPosition);

        JButton btnTopPosition = new JButton("top");
        btnTopPosition.setBounds(64, 266, 67, 23);
        btnTopPosition.setActionCommand("topPosition");
        btnTopPosition.addActionListener(this);
        getContentPane().add(btnTopPosition);

        JButton btnBottomPosition = new JButton("bottom");
        btnBottomPosition.setBounds(64, 345, 67, 23);
        btnBottomPosition.setActionCommand("bottomPosition");
        btnBottomPosition.addActionListener(this);
        getContentPane().add(btnBottomPosition);

        JLabel lblPosition = new JLabel("Position");
        lblPosition.setBounds(64, 241, 46, 14);
        getContentPane().add(lblPosition);

        JLabel lblLaser = new JLabel("Laser");
        lblLaser.setBounds(72, 412, 46, 14);
        getContentPane().add(lblLaser);

        JButton button = new JButton("top");
        button.setBounds(72, 437, 67, 23);
        button.setActionCommand("topLaser");
        button.addActionListener(this);
        getContentPane().add(button);

        JButton button_1 = new JButton("bottom");
        button_1.setBounds(72, 516, 67, 23);
        button_1.setActionCommand("bottomLaser");
        button_1.addActionListener(this);
        getContentPane().add(button_1);

        JButton button_2 = new JButton("right");
        button_2.setBounds(137, 477, 55, 23);
        button_2.setActionCommand("rightLaser");
        button_2.addActionListener(this);
        getContentPane().add(button_2);

        JButton button_3 = new JButton("left");
        button_3.setBounds(18, 477, 55, 23);
        button_3.setActionCommand("leftLaser");
        button_3.addActionListener(this);
        getContentPane().add(button_3);

        JButton btnCalibrate = new JButton("Calibrate");
        btnCalibrate.setBounds(6, 88, 89, 23);
        btnCalibrate.setActionCommand("calibrate");
        btnCalibrate.addActionListener(this);
        getContentPane().add(btnCalibrate);
    }

    /**
     * Get the JPanel that host the camera view.
     * 
     * @return JPanel
     */
    public GameScreen getScreenJPanel() {
        return gameScreen;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        if (e.getActionCommand().equals("init")) {
            try {
                this.moveMeClient = new MoveMeClient(this.getIpServer()
                        .getText(), 7899, 5566, this);
                this.moveMeClient.init();
                this.moveMeClient.setSphereTrackcolor(4 << 24, 4 << 24,
                        4 << 24, 4 << 24);
                this.moveMeClient.setStateDelay(34);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        } else if (e.getActionCommand().equals("moveController0Selected")) {
            moveControllerSelected = 0;
        } else if (e.getActionCommand().equals("moveController1Selected")) {
            moveControllerSelected = 1;
        } else if (e.getActionCommand().equals("moveController2Selected")) {
            moveControllerSelected = 2;
        } else if (e.getActionCommand().equals("moveController3Selected")) {
            moveControllerSelected = 3;
        } else if (e.getActionCommand().equals("topPosition")) {
            System.out.println("Top position");
            this.moveMeClient.positionTopPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("bottomPosition")) {
            System.out.println("Bottom position");
            this.moveMeClient.positionBottomPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("leftPosition")) {
            System.out.println("Left position");
            this.moveMeClient.positionLeftPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("rightPosition")) {
            System.out.println("Right position");
            this.moveMeClient.positionRightPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("topLaser")) {
            System.out.println("Top laser");
            this.moveMeClient.laserTopPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("bottomLaser")) {
            System.out.println("Bottom laser");
            this.moveMeClient.laserBottomPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("rightLaser")) {
            System.out.println("Right laser");
            this.moveMeClient.laserRightPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("leftLaser")) {
            System.out.println("Left laser");
            this.moveMeClient.laserLeftPlane(moveControllerSelected);
        } else if (e.getActionCommand().equals("calibrate")) {
            this.moveMeClient.calibrate(moveControllerSelected);
            this.moveMeClient.turnonPosition(moveControllerSelected);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void notifyError(Throwable t) {
        System.out.println(t.getMessage());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void processData(PSPayload payload) {
        PSMoveState moveState = (PSMoveState) payload;
        this.getLblValid()
                .setText(
                        "Valid:"
                                + moveState.movePositionPointerState[moveControllerSelected].valid);
        this.getLblNormalizedX()
                .setText(
                        "NormalizedX:"
                                + moveState.movePositionPointerState[moveControllerSelected].normalizedX);
        this.getLblNormalizedY()
                .setText(
                        "NormalizedY:"
                                + moveState.movePositionPointerState[moveControllerSelected].normalizedY);
        gameScreen.normalizedPointerX = moveState.movePositionPointerState[moveControllerSelected].normalizedX;
        gameScreen.normalizedPointerY = moveState.movePositionPointerState[moveControllerSelected].normalizedY;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void processImage(Image imageData) {
    }

    /**
     * Get the position panel that host the position of the enabled move
     * controllers.
     * 
     * @return JPanel position panel.
     */
    public JPanel getPositionPanel() {
        return spherePanel;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (e.getSource()
                .getClass()
                .equals(javax.swing.colorchooser.DefaultColorSelectionModel.class)) {
            Color newColor = this.getSphereColorChooser().getColor();
            float r = newColor.getRed() / 255.00f;
            float g = newColor.getGreen() / 255.00f;
            float b = newColor.getBlue() / 255.00f;
            System.out.println(r + " " + g + " " + b);
            this.moveMeClient.setSphereColor(moveControllerSelected, r, g, b);
        }
    }

    public JColorChooser getSphereColorChooser() {
        return sphereColorChooser;
    }

    public JTextField getIpServer() {
        return ipServer;
    }

    public static void main(String[] args) {
        NinjaFruitLike game = new NinjaFruitLike();
        game.pack();
        game.setVisible(true);
    }

    public JLabel getLblNormalizedX() {
        return lblNormalizedX;
    }

    public JLabel getLblNormalizedY() {
        return lblNormalizedY;
    }

    public JLabel getLblValid() {
        return lblValid;
    }
}